package com.mxx.v3.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mxx.v3.dao.mapper.TDeviceOeeMapper;
import com.mxx.v3.dao.mapper.TDeviceStatusMapper;
import com.mxx.v3.dto.*;

import com.mxx.v3.pojo.TDeviceOee;
import com.mxx.v3.pojo.TDeviceOeeExample;
import com.mxx.v3.pojo.TDeviceStatus;
import com.mxx.v3.pojo.TDeviceStatusExample;
import com.mxx.v3.service.DeviceOeeService;
import com.mxx.v3.utils.PageQuery;
import com.mxx.v3.web.query.DeviceOee0Query;
import com.mxx.v3.web.query.DeviceOee2Query;
import com.mxx.v3.web.query.DeviceOeeQuery;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.DecimalFormat;
import java.util.*;

/**
 * @program:report-from-server
 * @description:设备OEE报表的service实现类
 * @author:AlTuring
 * @Date:2019/11/4-11:42
 **/
@Service
public class DeviceOeeServiceImpl implements DeviceOeeService {

    @Autowired
    private TDeviceOeeMapper tDeviceOeeMapper;
    @Autowired
    private TDeviceStatusMapper tDeviceStatusMapper;

    @Override
    public PageInfo<DeviceOeeDto> queryDeviceOee(PageQuery<DeviceOeeQuery> deviceOeeQueryPageQuery) {
        //设置分页拦截
        Page<TDeviceOee> tDeviceOeePage = PageHelper.startPage(deviceOeeQueryPageQuery.getPage(), deviceOeeQueryPageQuery.getLimit(), true);
        //条件查询
        TDeviceOeeExample tDeviceOeeExample = new TDeviceOeeExample();
        TDeviceOeeExample.Criteria deviceCriteria = tDeviceOeeExample.createCriteria();
        //条件筛选
        DeviceOeeQuery deviceOeeQuery = deviceOeeQueryPageQuery.getQueryItem();
        //查询的时间（年、月）
        String yearMonth = deviceOeeQuery.getTime();
        //校验查询条件
        //设备编码
        if (!StringUtils.isEmpty(deviceOeeQuery.getCode())){
            deviceCriteria.andCodeEqualTo(deviceOeeQuery.getCode());
        }
        //设备类型
        if (!StringUtils.isEmpty(deviceOeeQuery.getType())){
            deviceCriteria.andTimeEqualTo(deviceOeeQuery.getType());
        }
        //设备型号
        if (!StringUtils.isEmpty(deviceOeeQuery.getModel())){
            deviceCriteria.andModelEqualTo(deviceOeeQuery.getModel());
        }
        deviceCriteria.andTimeLike(yearMonth+"%");
        //查询
        List<TDeviceOee> tDeviceOeeList = tDeviceOeeMapper.selectByExample(tDeviceOeeExample);

        Map<String,DeviceOeeDto> map = new HashMap<>();
        for (TDeviceOee tDeviceOee:tDeviceOeeList) {
            //判断是否该设备在Map是否已经存在
            //计算一个设备一个月的OEE，用设备编码来区分设备唯一性
            //FIXME 设备编码字段不能确定设备的唯一性
            if (map.containsKey(tDeviceOee.getCode())){
                DeviceOeeDto deviceOee = map.get(tDeviceOee.getCode());
                deviceOee.setWorkTime(Integer.valueOf(deviceOee.getWorkTime())+Integer.valueOf(tDeviceOee.getWorkTime()));
                deviceOee.setStandbyTime(Integer.valueOf(deviceOee.getStandbyTime())+Integer.valueOf(tDeviceOee.getStandbyTime()));
                deviceOee.setMachiningTime(Integer.valueOf(deviceOee.getMachiningTime())+Integer.valueOf(tDeviceOee.getMachiningTime()));
                deviceOee.setTotal(deviceOee.getTotal()+tDeviceOee.getTotal());
                deviceOee.setFailedNum(deviceOee.getFailedNum()+tDeviceOee.getFailednum());
            }else {
                DeviceOeeDto deviceOeeDto = DeviceOeeDto.builder()
                        .code(tDeviceOee.getCode())
                        .name(tDeviceOee.getName())
                        .model(tDeviceOee.getModel())
                        .type(tDeviceOee.getType())
                        .machiningTime(Integer.valueOf(tDeviceOee.getMachiningTime()))
                        .workTime(Integer.valueOf(tDeviceOee.getWorkTime()))
                        .standbyTime(Integer.valueOf(tDeviceOee.getStandbyTime()))
                        .total(Integer.valueOf(tDeviceOee.getTotal()))
                        .failedNum(Integer.valueOf(tDeviceOee.getFailednum()))
                        .time(yearMonth)
                        .build();
                map.put(deviceOeeDto.getCode(),deviceOeeDto);
            }
        }
        //遍历设备-查询设备每天的OEE数据
        List<DeviceOeeDto> list = new ArrayList<>();
        for (DeviceOeeDto deviceOeeDto: map.values()) {
            //条件查询
            TDeviceOeeExample tDeviceOeeExample1 = new TDeviceOeeExample();
            TDeviceOeeExample.Criteria criteria = tDeviceOeeExample1.createCriteria();
            if (!StringUtils.isEmpty(deviceOeeDto.getCode()))
            {
                criteria.andCodeEqualTo(deviceOeeDto.getCode());
            }
            if (!StringUtils.isEmpty(deviceOeeDto.getModel()))
            {
                criteria.andModelEqualTo(deviceOeeDto.getModel());
            }
            if (!StringUtils.isEmpty(deviceOeeDto.getType()))
            {
                criteria.andTypeEqualTo(deviceOeeDto.getType());
            }if (!StringUtils.isEmpty(deviceOeeDto.getTime()))
            {
                criteria.andTimeLike(deviceOeeDto.getTime()+"%");
            }
            //定义设备OEE的内容
            List<DeviceOeeContentDto> deviceOeeContentDtoList = new ArrayList<>();
            DeviceOeeContentDto workTime = new DeviceOeeContentDto();
            workTime.setName("计划开机时间");
            float[] workTimeArray = new float[31];
            DeviceOeeContentDto standbyTime = new DeviceOeeContentDto();
            standbyTime.setName("设备开机时间");
            float[] standbyTimeArray = new float[31];
            DeviceOeeContentDto machiningTime = new DeviceOeeContentDto();
            machiningTime.setName("作业时间");
            float[] machiningTimeArray = new float[31];
            DeviceOeeContentDto total = new DeviceOeeContentDto();
            total.setName("总零件数");
            float[] totalArray = new float[31];
            DeviceOeeContentDto failedNum = new DeviceOeeContentDto();
            failedNum.setName("不合格的数量");
            float[] failNumArray = new float[31];
            DeviceOeeContentDto deviceUtilization = new DeviceOeeContentDto();
            deviceUtilization.setName("设备开动率(%)");
            float[]  deviceUtilizationArray= new float[31];
            DeviceOeeContentDto deviceEffectiveness = new DeviceOeeContentDto();
            deviceEffectiveness.setName("设备负荷效率(%)");
            float[] deviceEffectivenessArray = new float[31];
            DeviceOeeContentDto passRate = new DeviceOeeContentDto();
            passRate.setName("合格率(%)");
            float[] passRateArray = new float[31];
            DeviceOeeContentDto oee = new DeviceOeeContentDto();
            oee.setName("OEE(%)");
            float[] oeeArray = new float[31];
            //查询-当前设备这个月每天的数据
            List<TDeviceOee> tDeviceOees = tDeviceOeeMapper.selectByExample(tDeviceOeeExample1);
            DecimalFormat df=new DecimalFormat("0.00");//设置保留位数
            for (TDeviceOee t: tDeviceOees) {
                //获取当前时间，计算放在数组的位置，数组的代表一个月的31天（2019-09-11 -> 11）
                String day = t.getTime().split("-")[2];
                //计算出需要存放在数组的什么位置
                int num = Integer.valueOf(day) - 1;
                workTimeArray[num] = Float.valueOf(t.getWorkTime());
                standbyTimeArray[num] = Float.valueOf(t.getStandbyTime());
                machiningTimeArray[num] = Float.valueOf(t.getMachiningTime());
                totalArray[num] = Float.valueOf(t.getTotal());
                failNumArray[num] = Float.valueOf(t.getFailednum());
                deviceUtilizationArray[num] = (standbyTimeArray[num] + machiningTimeArray[num]) / workTimeArray[num]*100;
                deviceEffectivenessArray[num] = machiningTimeArray[num] / (standbyTimeArray[num] + machiningTimeArray[num])*100;
                if (totalArray[num] != 0 && failNumArray[num] != 0){
                    passRateArray[num] = (totalArray[num] - failNumArray[num]) / totalArray[num]*100;
                }else if (totalArray[num] != 0 && failNumArray[num] == 0){
                    passRateArray[num] = 100;
                }else {
                    passRateArray[num] = 0;
                }

                oeeArray[num] = deviceUtilizationArray[num] * deviceEffectivenessArray[num] * passRateArray[num]/100/100;
            }
            //统计每天的
            workTime.setDayArray(workTimeArray);
            standbyTime.setDayArray(standbyTimeArray);
            machiningTime.setDayArray(machiningTimeArray);
            total.setDayArray(totalArray);
            failedNum.setDayArray(failNumArray);
            deviceUtilization.setDayArray(deviceUtilizationArray);
            deviceEffectiveness.setDayArray(deviceEffectivenessArray);
            passRate.setDayArray(passRateArray);
            oee.setDayArray(oeeArray);
            //设置月统计的总和
            workTime.setMonthTotal(deviceOeeDto.getWorkTime());
            standbyTime.setMonthTotal(deviceOeeDto.getStandbyTime());
            machiningTime.setMonthTotal(deviceOeeDto.getMachiningTime());
            total.setMonthTotal(deviceOeeDto.getTotal());
            failedNum.setMonthTotal(deviceOeeDto.getFailedNum());
            if(deviceOeeDto.getWorkTime() != 0){
                deviceUtilization.setMonthTotal((deviceOeeDto.getStandbyTime() + deviceOeeDto.getMachiningTime()) / deviceOeeDto.getWorkTime());
            }else {
                deviceUtilization.setMonthTotal(0);
            }
            if(deviceOeeDto.getMachiningTime()!=0&&deviceOeeDto.getMachiningTime()!=null&&deviceOeeDto.getStandbyTime()!=0&&deviceOeeDto.getStandbyTime()!=null&&deviceOeeDto.getMachiningTime()!=0&&deviceOeeDto.getMachiningTime()!=null){
                deviceEffectiveness.setMonthTotal( deviceOeeDto.getMachiningTime() / (deviceOeeDto.getStandbyTime() + deviceOeeDto.getMachiningTime()));
            }
            if (deviceOeeDto.getTotal() != 0 && deviceOeeDto.getFailedNum() != 0){
                passRate.setMonthTotal((total.getMonthTotal() - failedNum.getMonthTotal()) - total.getMonthTotal());
                //性能效率（开动率）× 利用率（负荷率）×合格率
                oee.setMonthTotal(deviceUtilization.getMonthTotal() * deviceEffectiveness.getMonthTotal() * passRate.getMonthTotal());
            }else if(deviceOeeDto.getTotal() != 0 && deviceOeeDto.getFailedNum() == 0&&deviceUtilization.getMonthTotal()!=null&&deviceEffectiveness.getMonthTotal()!=null)
            {
                //当失败件数为0表示合格率100%；
                oee.setMonthTotal(deviceUtilization.getMonthTotal() * deviceEffectiveness.getMonthTotal());
            }
            else {
                passRate.setMonthTotal(0);
                oee.setMonthTotal(0);
            }

            deviceOeeContentDtoList.add(workTime);
            deviceOeeContentDtoList.add(standbyTime);
            deviceOeeContentDtoList.add(machiningTime);
            deviceOeeContentDtoList.add(total);
            deviceOeeContentDtoList.add(failedNum);
            deviceOeeContentDtoList.add(deviceUtilization);
            deviceOeeContentDtoList.add(deviceEffectiveness);
            deviceOeeContentDtoList.add(passRate);
            deviceOeeContentDtoList.add(oee);

            deviceOeeDto.setList(deviceOeeContentDtoList);
            list.add(deviceOeeDto);
        }
        //封装成PageInfo
        PageInfo<DeviceOeeDto> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(tDeviceOeePage.getTotal());

        return pageInfo;
    }
    @Override
    public PageInfo<DeviceOeeStatusDto> queryDeviceOee0(PageQuery<DeviceOee0Query> deviceOee0QueryPageQuery){
       //设置分页拦截
        Page<TDeviceStatus> tDeviceStatusPage = PageHelper.startPage(deviceOee0QueryPageQuery.getPage(), deviceOee0QueryPageQuery.getLimit(), true);
        //条件查询1
        TDeviceStatusExample tDeviceStatusExample = new TDeviceStatusExample();
        TDeviceStatusExample.Criteria devticeCriteria = tDeviceStatusExample.createCriteria();
        //条件查询2
        TDeviceOeeExample tDeviceOeeExample = new TDeviceOeeExample();
        TDeviceOeeExample.Criteria deviceCriteria = tDeviceOeeExample.createCriteria();
        //条件筛选
        DeviceOee0Query deviceOee0Query = deviceOee0QueryPageQuery.getQueryItem();
        //查询的时间（年、月）
        String year = deviceOee0Query.getTime();
        //设备型号
        if (!StringUtils.isEmpty(year)){
            devticeCriteria.andTimeLike(year+"%");
            deviceCriteria.andTimeLike(year+"%");
        }
        //查询
        List<TDeviceStatus> deviceStatusList= tDeviceStatusMapper.selectByExample(tDeviceStatusExample);
        //查询
        List<TDeviceOee> tDeviceOees = tDeviceOeeMapper.selectByExample(tDeviceOeeExample);

        //封装成PageInfo
        PageInfo<DeviceOeeStatusDto> pageInfo = new PageInfo<>(getListOrderByMouth(deviceStatusList,tDeviceOees));
        pageInfo.setTotal(tDeviceStatusPage.getTotal());
        return pageInfo;
    }
    ////下面是queryDeviceOee0的功能方法
    //这是获取每个月的具体数据
     private List<DeviceOeeStatusDto> getListOrderByMouth(List<TDeviceStatus> deviceStatusList,List<TDeviceOee> tDeviceOees){
        List<DeviceOeeStatusDto> list=new ArrayList<>();
        Map<Integer,DeviceOeeStatusDto> map=new LinkedHashMap<>();
        //先创建12月的基础数据
        for(int i=12;i >= 1;i--) {
            DeviceOeeStatusDto temp=new DeviceOeeStatusDto();
            temp.setMonth(String.valueOf(i)+"月");
            temp.setDeviceIntactNum(0);
            temp.setActualRunNum(0);
            temp.setDeviceTotal(0);
            temp.setActualRunTime(0);
            temp.setPlanRunTime(0);
            temp.setMalfunctionTime(0);
            temp.setStopTime(0);
            temp.setProductTotal(0);
            temp.setProductFailedNum(0);
            temp.setActualStandbyTime(0);
            temp.setActualMachiningTime(0);
            temp.setOeeAverage("0");
            temp.setDeviceIntactRate("");
            temp.setDeviceMalfunctionRate("");
            temp.setDeviceStopRate("");
            temp.setDeviceUseRate("");
            map.put(i,temp);
        }
        //轮询加入数据
        for(TDeviceStatus item:deviceStatusList)
        {
            Integer mouth=Integer.valueOf(item.getTime().split("-")[1]);
            DeviceOeeStatusDto temp=map.get(mouth);
            ////设备总台数
            temp.setDeviceTotal(temp.getDeviceTotal()+item.getSum());
            // //设备完好
            temp.setDeviceIntactNum(temp.getDeviceIntactNum()+item.getPerfect());
            //设备运行台数
            temp.setActualRunNum(temp.getDeviceIntactNum()+item.getStartup());

            map.put(Integer.valueOf(item.getTime().split("-")[1]),temp);
        }
        //补充，其他字段
         map=getMore(tDeviceOees,map);
         DecimalFormat df=new DecimalFormat("0.00");//设置保留位数
        for(DeviceOeeStatusDto item:map.values())
        {
            //计算设备利用率
            if(item.getActualRunTime()!=0&&item.getActualMachiningTime()!=0)
                item.setDeviceStartUpRate(df.format((double)item.getActualRunTime()/item.getPlanRunTime()*100));
            //计算设备利用率
            if(item.getPlanRunTime()!=0&&item.getActualRunNum()!=0)
            item.setDeviceUseRate(df.format((double)item.getActualMachiningTime()/item.getActualRunTime()*100));
            //设备完好率
            if(item.getDeviceIntactNum()!=0&&item.getDeviceTotal()!=0)
            item.setDeviceIntactRate(df.format((double)item.getDeviceIntactNum()/item.getDeviceTotal()*100));
            //设备故障率
            if(item.getMalfunctionTime()!=0&&item.getPlanRunTime()!=0)
            item.setDeviceMalfunctionRate(df.format((double)item.getMalfunctionTime()/item.getPlanRunTime()*100));
            //设备的平均效率,但是这个计算方式时有问题的。
             if(item.getDeviceIntactRate()!=""&&item.getDeviceUseRate()!="0"&&item.getProductTotal()!=0)
             {
             //计算合格率
             double qulifideproduct=item.getProductTotal()-item.getProductFailedNum();
             double qulifide=qulifideproduct/item.getProductTotal();
             //设备平均OEE
             //性能效率（开动率）× 利用率（负荷率）×合格率
             item.setOeeAverage(df.format(Double.valueOf( item.getDeviceUseRate())*Double.valueOf(item.getDeviceStartUpRate())*qulifide/100));
             }
            list.add(item);
        }
        return list;
     }
     //下面是根据传入的列表，查出计划时间等待
     private Map<Integer,DeviceOeeStatusDto> getMore(List<TDeviceOee> tDeviceOees,Map<Integer,DeviceOeeStatusDto> map) {
         for (TDeviceOee item:tDeviceOees) {
             Integer mouth=Integer.valueOf(item.getTime().split("-")[1]);
             DeviceOeeStatusDto temp=map.get(mouth);
             //每月加工时间
             if(item.getMachiningTime()!="")
             temp.setActualMachiningTime(temp.getActualMachiningTime()+Integer.parseInt(item.getMachiningTime()));
             //每月待机时间
             if(item.getStandbyTime()!="")
                 temp.setActualStandbyTime(temp.getActualStandbyTime()+Integer.parseInt(item.getStandbyTime()));
             //计划加工时间
             if(item.getWorkTime()!="")
                 temp.setPlanRunTime(temp.getPlanRunTime()+Integer.parseInt(item.getWorkTime()));
             //实际运行时间
             if(!StringUtils.isEmpty(item.getStandbyTime())&&!StringUtils.isEmpty(item.getMachiningTime()))
             temp.setActualRunTime(temp.getActualRunTime()+Integer.parseInt(item.getStandbyTime())+Integer.parseInt(item.getMachiningTime()));
             //故障时间
             if(item.getMachiningTime()!="")
             temp.setMalfunctionTime(temp.getMalfunctionTime());
             //计算工件总数
             if(item.getTotal()!=0)
             temp.setProductTotal(temp.getProductTotal()+item.getTotal());
             //计算工件总失败数
             if(item.getFailednum()!=0)
              temp.setProductFailedNum(temp.getProductFailedNum()+item.getFailednum());
             //其他停机时间
             temp.setStopTime(0);
             // temp
             map.put(mouth,temp);
         }
         return map;
     }
    ///下面是queryDeviceOee0的功能结束
    @Override
    public PageInfo<DeviceMouthStatusDto> queryDeviceOee2(PageQuery<DeviceOee2Query> deviceOee2QueryPageQuery){
        //设置分页拦截
      //  Page<DeviceMouthStatusDto> tDeviceStatusDtoPage = PageHelper.startPage(deviceOee2QueryPageQuery.getPage(), deviceOee2QueryPageQuery.getLimit(), true);
        //条件查询2
        TDeviceOeeExample tDeviceOeeExample = new TDeviceOeeExample();
        TDeviceOeeExample.Criteria deviceCriteria = tDeviceOeeExample.createCriteria();
        //条件筛选
        DeviceOee2Query deviceOee2Query = deviceOee2QueryPageQuery.getQueryItem();
        //设备程序时间
        if (!StringUtils.isEmpty(deviceOee2Query.getTime())){
            deviceCriteria.andTimeLike(deviceOee2Query.getTime()+"%");
        }
        if(!StringUtils.isEmpty(deviceOee2Query.getType()))
        {
            deviceCriteria.andTypeLike("%"+deviceOee2Query.getType()+"%");
        }
        if(!StringUtils.isEmpty(deviceOee2Query.getModel()))
        {
            deviceCriteria.andModelLike("%"+deviceOee2Query.getModel()+"%");
        }
        if(!StringUtils.isEmpty(deviceOee2Query.getCode()))
        {
            deviceCriteria.andCodeLike("%"+deviceOee2Query.getCode()+"%");
        }
        //查询
        List<TDeviceOee> tDeviceOees = tDeviceOeeMapper.selectByExample(tDeviceOeeExample);
        Map<String,DeviceMouthStatusDto> tDeviceOeeMap=new HashMap<>();
        //下面设置第一个list对象
        for(TDeviceOee tDeviceOee:tDeviceOees)
        {
            if(tDeviceOee.getCode()!=""&&!tDeviceOeeMap.containsKey(tDeviceOee.getCode()))
            {
                DeviceMouthStatusDto deviceMouthStatusDtofirst=new DeviceMouthStatusDto();
                deviceMouthStatusDtofirst.setCode(tDeviceOee.getCode());
                deviceMouthStatusDtofirst.setModel(tDeviceOee.getModel());
                deviceMouthStatusDtofirst.setName(tDeviceOee.getName());
                deviceMouthStatusDtofirst.setType(tDeviceOee.getType());
                deviceMouthStatusDtofirst.setList(deviceMouthStatusDtofirst.createList());
                tDeviceOeeMap.put(tDeviceOee.getCode(),deviceMouthStatusDtofirst);
            }
        }
        DecimalFormat df=new DecimalFormat("0.00");//设置保留位数
        for(TDeviceOee tDeviceOee:tDeviceOees)
        {
            DeviceMouthStatusDto temp=tDeviceOeeMap.get(tDeviceOee.getCode());
            List<DeviceMouthStatusDto> tempList= temp.getList();
            for(DeviceMouthStatusDto item:tempList)
            {
                int arrayItem=Integer.parseInt(tDeviceOee.getTime().split("-")[2])-1;
                String[] tem=item.getDayArray();
                if(item.getItem()=="计划开机时间"&&tDeviceOee.getWorkTime()!=null){
                    tem[arrayItem]=tDeviceOee.getWorkTime();
                    item.setMonthTotal(df.format(Double.valueOf(tem[arrayItem])+Double.valueOf(item.getMonthTotal())));
                    item.setMouthTotalHour(df.format(Double.valueOf(item.getMonthTotal())/60));
                }
                if(item.getItem()=="开机时间"&&tDeviceOee.getStandbyTime()!=""){
                    tem[arrayItem]=String.valueOf(Integer.parseInt(tDeviceOee.getMachiningTime())+Integer.parseInt(tDeviceOee.getStandbyTime()));
                    item.setMonthTotal(df.format(Double.valueOf(tem[arrayItem])+Double.valueOf(item.getMonthTotal())));
                    item.setMouthTotalHour(df.format(Double.valueOf(item.getMonthTotal())/60));
                }
                if(item.getItem()=="加工时间"&&tDeviceOee.getMachiningTime()!=""){
                    tem[arrayItem]=String.valueOf(tDeviceOee.getMachiningTime());
                    item.setMonthTotal(df.format(Double.valueOf(tem[arrayItem])+Double.valueOf(item.getMonthTotal())));
                    item.setMouthTotalHour(df.format(Double.valueOf(item.getMonthTotal())/60));
                }
                if(item.getItem()=="故障时间"&&tDeviceOee.getMalfunctionTime()!=null){
                    tem[arrayItem]=tDeviceOee.getMalfunctionTime().toString();
                    item.setMonthTotal(df.format(Double.valueOf(tem[arrayItem])+Double.valueOf(item.getMonthTotal())));
                    item.setMouthTotalHour(df.format(Double.valueOf(item.getMonthTotal())/60));
                }
                if(item.getItem()=="其他停机时间"){
                    tem[arrayItem]="0";
                    item.setDayArray(tem);
                    item.setMonthTotal(df.format(Double.valueOf(tem[arrayItem])+Double.valueOf(item.getMonthTotal())));
                    item.setMouthTotalHour(df.format(Double.valueOf(item.getMonthTotal())/60));
                }
                if(item.getItem()=="设备开动率(%)"&&tDeviceOee.getMachiningTime()!=""){
                     Integer mashinetime=Integer.parseInt(tDeviceOee.getMachiningTime());
                     Integer allTime=Integer.parseInt(tDeviceOee.getStandbyTime())+Integer.parseInt(tDeviceOee.getMachiningTime());
                     if(mashinetime!=0&&allTime!=0)
                     {
                         tem[arrayItem]=df.format((double)mashinetime/allTime*100);
                         item.setMonthTotal(df.format(Double.valueOf(tem[arrayItem])+Double.valueOf(item.getMonthTotal())));
                     }
                }
                if(item.getItem()=="设备负荷率(%)"&&tDeviceOee.getStandbyTime()!=""&&tDeviceOee.getMachiningTime()!=""&&tDeviceOee.getWorkTime()!=""){
                    Integer start=Integer.parseInt(tDeviceOee.getStandbyTime())+Integer.parseInt(tDeviceOee.getMachiningTime());
                    Integer planStart=Integer.parseInt(tDeviceOee.getWorkTime());
                    if(start!=0&&planStart!=0)
                    {
                        tem[arrayItem]=df.format((double)start/planStart*100);
                        item.setMonthTotal(df.format(Double.valueOf(tem[arrayItem])+Double.valueOf(item.getMonthTotal())));
                    }
                }
                if(item.getItem()=="设备故障停机率(%)"&&tDeviceOee.getWorkTime()!=""&&tDeviceOee.getMalfunctionTime()!=null){

                    Integer malTime=tDeviceOee.getMalfunctionTime().intValue();
                    Integer planStart=Integer.parseInt(tDeviceOee.getWorkTime());
                    if(malTime!=0&&planStart!=0)
                    {
                        tem[arrayItem]=df.format((double)malTime/planStart*100);
                        item.setMonthTotal(df.format(Double.valueOf(tem[arrayItem])+Double.valueOf(item.getMonthTotal())));
                    }
                }
                item.setDayArray(tem);
             //   System.out.println( tDeviceOee.getCode()+"   "+item.getItem()+arrayItem+":"+tem[arrayItem]);
            }
            temp.setList(tempList);
            tDeviceOeeMap.put(tDeviceOee.getCode(),temp);
        }
        List<DeviceMouthStatusDto> deviceMouthStatusDtoList=new ArrayList<>();
        for(DeviceMouthStatusDto temp:tDeviceOeeMap.values()) {
            for (DeviceMouthStatusDto item : temp.getList())
            {
                int kaidonglu = 0, fuhelu = 0, guzhanglu = 0;
            if (item.getItem() == "设备开动率(%)" && item.getMonthTotal() != "0") {
                for (String str : item.getDayArray())
                    if(str!=null) kaidonglu++;
                 if(kaidonglu>0)
                item.setMonthTotal(df.format(Double.valueOf(item.getMonthTotal()) / kaidonglu));
            }
            if (item.getItem() == "设备负荷率(%)" && item.getMonthTotal() != "0") {
                for (String str : item.getDayArray())
                    if(str!=null) fuhelu++;
                 if(fuhelu>0)
                item.setMonthTotal(df.format(Double.valueOf(item.getMonthTotal()) /fuhelu));
            }
            if (item.getItem() == "设备故障停机率(%)" && item.getMonthTotal() != "0") {
                for (String str : item.getDayArray())
                    if(str!=null)  guzhanglu++;
                if(guzhanglu>0)
                item.setMonthTotal(df.format(Double.valueOf(item.getMonthTotal()) / guzhanglu));
            }
        }
        }
        //封装成PageInfo
        PageInfo<DeviceMouthStatusDto> pageInfo = new PageInfo<>(new ArrayList(tDeviceOeeMap.values()));
      //  pageInfo.setTotal(tDeviceStatusDtoPage.getTotal());
        return pageInfo;
    }
   //获取表头所有分类的方法
    @Override
    public PageInfo<DeviceClassify> getDeviceClassify(){
        Set<String> setCode=new HashSet<>();
        Set<String> setModle=new HashSet<>();
        Set<String> setType=new HashSet<>();
        //条件查询
        TDeviceOeeExample tDeviceOeeExample = new TDeviceOeeExample();
        TDeviceOeeExample.Criteria deviceCriteria = tDeviceOeeExample.createCriteria();
        //查询
        List<TDeviceOee> tDeviceOeeList = tDeviceOeeMapper.selectByExample(tDeviceOeeExample);
        for(TDeviceOee item:tDeviceOeeList){
            setCode.add(item.getCode());
            setModle.add(item.getModel());
            setType.add(item.getType());
        }
        DeviceClassify deviceClassify=new DeviceClassify();
                String[] str=new String[1];
        deviceClassify.setCode(setCode.toArray(str));
        deviceClassify.setModel(setModle.toArray(str));
        deviceClassify.setType( setType.toArray(str));
        List<DeviceClassify> list=new ArrayList<>();
        list.add(deviceClassify);
        PageInfo<DeviceClassify> pageInfo = new PageInfo<>(list);
        return pageInfo;
        //return deviceClassify;
    }

}
